package com.jxsd.edu.cn.struts2.actions;

import java.util.HashMap;
import java.util.List;

import com.jxsd.edu.cn.struts2.models.Category;
import com.jxsd.edu.cn.struts2.models.PubMsgview;
import com.jxsd.edu.cn.struts2.models.Topic;
import com.jxsd.edu.cn.struts2.serviceImpl.IndexServiceImpl;
import com.jxsd.edu.cn.struts2.serviceImpl.TopicServiceImpl;
import com.jxsd.edu.cn.struts2.services.IndexService;
import com.jxsd.edu.cn.struts2.services.TopicService;
import com.jxsd.edu.cn.tools.Page;
import com.jxsd.edu.cn.tools.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class IndexAction  extends  ActionSupport 
{                 
	private static final long serialVersionUID = 1L;
	
	//一级类别信息
	private     List<Category>   list;
	//最新区域性记录
	private     HashMap<String,Page<PubMsgview>> map;
	//功能
	private     IndexService   service=new  IndexServiceImpl();
	//求购吧服务
	private     TopicService   topicService=new  TopicServiceImpl();
	//城市名
	private    String  cityName;
	//求购帖子
	private     List<Topic>  topics;
	
	public   IndexAction()
	{
		map=new  HashMap<String,Page<PubMsgview>>();
		map.put("1", null);
		map.put("2", null);
		map.put("3", null);
		map.put("4", null);
		cityName=(String)ActionContext.getContext().getSession().get("cityName");
	}
	
	@Override
	public String execute() throws Exception 
	{
        list=service.getAllCatagory();
        getPages();
        topics=topicService.queryNewlyTopic(cityName);
		return SUCCESS;
	}
	
	private  void  getPages()
	{
		PageBean<PubMsgview>  pagebean=new  PageBean<PubMsgview>();
		pagebean.setPageSize(8);
		for(String  catagoryId:map.keySet())
		{
			//取得数据
			Page<PubMsgview>     page=service.getPagePubMsg(catagoryId, pagebean,cityName);
	        //将数据放进集合
	        map.put(catagoryId, page);
		}
	}

	public List<Category> getList() {
		return list;
	}

	public void setList(List<Category> list) {
		this.list = list;
	}

	public HashMap<String,Page<PubMsgview>> getMap() {
		return map;
	}

	public void setMap(HashMap<String,Page<PubMsgview>> map) {
		this.map = map;
	}

	public String getCityName() {
		return cityName;
	}

	public void setCityName(String cityName) {
		this.cityName = cityName;
	}

	public List<Topic> getTopics() {
		return topics;
	}

	public void setTopics(List<Topic> topics) {
		this.topics = topics;
	}
}
